/***************************************************************************
 *   Copyright (C) 2007 by Marcin Swiechowicz   *
 *   marcin.swiechowicz@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "kdcstreammodedelegate.h"

#include <kdcdisc.h>

#include <QModelIndex>
#include <QVariant>
#include <QList>

#include <KComboBox>
#include <KLocale>

KDCStreamModeDelegate::KDCStreamModeDelegate( QObject *parent )
	: QItemDelegate( parent ), p_movie( 0 ), p_id( 0 )
{

}

void KDCStreamModeDelegate::setCurrentVideoData( const KDCMovie *movie )
{
	p_movie = movie;
	p_mode = KDCStreamModeDelegate::Video;
}

void KDCStreamModeDelegate::setCurrentAudioData( const KDCMovie *movie, int id )
{
	p_movie = movie;
	p_id = id;
	p_mode = KDCStreamModeDelegate::Audio;
}

QWidget* KDCStreamModeDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	Q_UNUSED( option );
	Q_UNUSED( index );
	KComboBox *editor = new KComboBox( parent );
	KDC::DiscType type = p_movie->structure()->disc()->type();
	KDC::StreamMode streamMode;
	int i;
	QList<bool> copy;
	if ( p_mode == Video )
	{
		streamMode = p_movie->videoStream()->mode();
		QList<KDCVideoStreamMetaData*> metaData;
		if ( p_movie->movieGroup() )
		{
			KDCMovieGroup *group = p_movie->movieGroup();
			for ( i=0; i<group->count(); ++i )
				metaData.append( group->movie( i )->videoStream()->metaData() );
		}
		else
			metaData.append( p_movie->videoStream()->metaData() );
		if ( type == KDC::DVD )
		{
			for ( i=0; i<metaData.size(); ++i )
				copy.append( KDCVideoStreamMetaData::isDVDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isDVDNTSCVideoStream( metaData.at( i ) ) );
		}
		else
			if ( type == KDC::CVD )
			{
				for ( i=0; i<metaData.size(); ++i )
					copy.append( KDCVideoStreamMetaData::isCVDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isCVDNTSCVideoStream( metaData.at( i ) ) );
			}
		else
			if ( type == KDC::SVCD )
			{
				for ( i=0; i<metaData.size(); ++i )
					copy.append( KDCVideoStreamMetaData::isSVCDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isSVCDNTSCVideoStream( metaData.at( i ) ) );
			}
		else
			if ( type == KDC::VCD )
			{
				for ( i=0; i<metaData.size(); ++i )
					copy.append( KDCVideoStreamMetaData::isVCDPALVideoStream( metaData.at( i ) ) || KDCVideoStreamMetaData::isVCDNTSCVideoStream( metaData.at( i ) ) );
			}
		copy.removeAll( TRUE );
		if ( copy.isEmpty() )
			editor->addItem( "Copy", KDC::Copy );
	}
	else
	{
		streamMode = p_movie->audioStreamGroup()->stream( p_id )->mode();
		QList<KDCAudioStreamMetaData*> metaData;
		if ( p_movie->movieGroup() )
		{
			KDCMovieGroup *group = p_movie->movieGroup();
			for ( i=0; i<group->count(); ++i )
				metaData.append( group->movie( i )->audioStreamGroup()->stream( p_id )->metaData() );
		}
		else
			metaData.append( p_movie->audioStreamGroup()->stream( p_id )->metaData() );
		if ( type == KDC::DVD )
		{
			for ( i=0; i<metaData.size(); ++i )
				if ( p_movie->videoFormat() == KDC::PAL )
					copy.append( KDCAudioStreamMetaData::isDVDPALAudioStream( metaData.at( i ) ) );
				else
					copy.append( KDCAudioStreamMetaData::isDVDNTSCAudioStream( metaData.at( i ) ) );
		}
		else
			if ( type == KDC::CVD )
			{
				for ( i=0; i<metaData.size(); ++i )
					copy.append( KDCAudioStreamMetaData::isCVDAudioStream( metaData.at( i ) ) );
			}
		else
			if ( type == KDC::SVCD )
			{
				for ( i=0; i<metaData.size(); ++i )
					copy.append( KDCAudioStreamMetaData::isSVCDAudioStream( metaData.at( i ) ) );
			}
		else
			if ( type == KDC::VCD )
			{
				for ( i=0; i<metaData.size(); ++i )
					copy.append( KDCAudioStreamMetaData::isVCDAudioStream( metaData.at( i ) ) );
			}
		copy.removeAll( TRUE );
		if ( copy.isEmpty() )
			editor->addItem( i18n("Copy"), KDC::Copy );
	}
	editor->addItem( i18n("Resample"), KDC::Resample );
	editor->setCurrentIndex( editor->findData( streamMode ) );
	connect( editor, SIGNAL( currentIndexChanged( int ) ), this, SLOT( slotCurrentIndexChanged( int ) ) );
	return editor;
}

void KDCStreamModeDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex& index ) const
{
	KComboBox *tmp = static_cast<KComboBox*>( editor );
	model->setData( index, tmp->currentText() );
}

void KDCStreamModeDelegate::updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
	Q_UNUSED( index );
	editor->setGeometry( option.rect );
}

void KDCStreamModeDelegate::slotCurrentIndexChanged( int index )
{
	KComboBox *editor = qobject_cast<KComboBox*>( sender() );
	if ( editor )
	{
		if ( editor->itemData( index ).toInt() == KDC::Copy )
			emit modeChanged( KDC::Copy );
		else
			emit modeChanged( KDC::Resample );
	}
}
